package main
import (
    "os/exec"
    "strings"
    "labix.org/v2/mgo/bson"
    "fmt"
)

var (
    TaskQueue = make(chan func(),100)
)

func Worker() {
    for true {
	select {
	    case c := <-TaskQueue:
		c()
	}
    }
}

func Job_Builder(apk_path,interf string,c chan string) func() {
    return func() {
	output,err := exec.Command("java","-jar","att.jar",interf,apk_path).CombinedOutput()
	if err != nil {
	    fmt.Print(string(output))
	    panic(err)
	}
	output_list := strings.Split(string(output),"\n")
	c <- output_list[len(output_list) - 1]
	fmt.Print("Finish Analyze" + interf)
    }
}

func Add_Task(apk_path string) bson.ObjectId{
    ID := NewID()
    TaskQueue <- func () {
	chan_3g := make(chan string,1)
	chan_wifi := make(chan string,1)
	go Job_Builder(apk_path,"3G",chan_3g)()
	go Job_Builder(apk_path,"WIFI",chan_wifi)()
	file_3g := <-chan_3g
	file_wifi := <-chan_wifi
	result := Analyze(file_3g,file_wifi)
	result.ID = ID
	DB_AnalyzeData.Insert(result)
    }
    return ID
}
